package com.knight.concurrent.chapter8;

/**
 * @Description: 应用程序注入钩子
 * @author: knight
 * @Date: 2019-03-09 10:09
 */
public class ExitCapture {

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("The application will be exit");
        }));

        int i = 0;
        while (true) {
            try {
                Thread.sleep(1000);
                System.out.println("I'm working");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            i++;
            if (i > 20) {
                throw new RuntimeException("error");
            }

        }
    }
}
